$.namespace('squareActivity');

$(function(){
	squareActivity.init();
})

squareActivity = function(){
	var url = '',isLoaded = false;
	return {
		init : function(){
			EasyUIInit.initControl(this.config.grid);
			EasyUIInit.initControl(this.config.newDlg);
			EasyUIInit.initControl(this.config.searchDlg);
		},
		showForm : function(){
			$('#newDlg').dialog('open');
			$('#fm').form('clear');
			url = ctxRest + '/grid/squareActivity/create';
		},
		showEditForm : function(id){			
			if (!id) {
				$.messager.alert('提示', '请选择要查看的数据', 'info');
				return;
			}
			$('#newDlg').dialog('open');
			url = ctxRest + '/grid/squareActivity/update';
			$.ajax({
				type: 'GET',
				url : ctxRest + '/grid/squareActivity/get?id=' + id,
				contentType : 'application/json;charset=utf-8',
				beforeSend : function(){
					progressLoad();
				},
				success : function(result){
					progressClose();//console.log(result);
					$('#regionID').combobox('clear');
					 $.ajax(
		             	{
		             		type : 'GET',
		                   	url : ctx + '/rest/gcb/grid/findRegionById?id='+result.gridID,
		                   	contentType : 'application/json;charset=UTF-8',
		                   	async:false,
		                   	success : function(result){
		           				$('#regionID').combobox('loadData',result);
		           			}
		                }
		             );
					$('#fm').form('load', result);
				}
			})
		},
		remove : function(){
			var row = $('#grid').datagrid('getSelected');
			if (!row) {
				$.messager.alert('提示', '请选择要删除的信息', 'info')
				return;
			}
			$.messager.confirm('提示', '您确定要删除选中的信息？',function(flag){
				if (flag) {
					$.ajax({
						type : 'GET',
						contentType : 'application/json;charset=UTF-8',
						url	: ctxRest + '/grid/squareActivity/delete?id=' + row.id,
						beforeSend : function(){progressLoad();},
						success : function(result){
							progressClose();
							if (result && result.errcode == 0) {
								$.messager.alert('提示', '操作成功！', 'info');
								$('#grid').datagrid('reload');
							}
						}
					});
				}
			});
		},
		save : function(){
			var obj = this.getFormData();
			if (obj){
				progressLoad();
				$.ajax({
					url : url,
					type : 'POST',
					contentType : 'application/json;charset=UTF-8',
					data : JSON.stringify(obj),
					success : function(result){
						progressClose();
						$('#newDlg').dialog('close');
						$('#grid').datagrid('reload');
					}
				})
			}
		},
		getFormData : function(){
			var fm = $('#fm');
			if (fm.form('validate')) {
				var obj = $.serializeObject(fm);
				return obj;
			}
		},
		showSearchForm : function(){
			$('#searchDlg').dialog('open');
		},
		search : function(obj){
			$('#grid').datagrid({ queryParams: this.form2Json() }); 
			$('#searchDlg').dialog('close');
		},
		clearSearch : function(){
			$('#searchfm').form('clear');
		},
		form2Json : function(id){
			 
            var arr = $('#searchfm').serializeArray();
            var jsonStr = "";
 
            jsonStr += '{';
            for (var i = 0; i < arr.length; i++) {
                jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",';
            }
            jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
            jsonStr += '}';
 
            var json = JSON.parse(jsonStr);
            return json;
        },
        
        handleSelect:function(record)
		 {
			 $('#regionID').combobox('clear');
			 $('#searchfm').find('select[id=regionID]').combobox('clear');
			 switch(type)
			 {
			 	default:
			 	    //这里可以手动通过ajax和js对象筛选数据
                   var newData = $.ajax(
                   		{
                   			type : 'POST',
                   			data : JSON.stringify(record),
                   			url : ctx + '/rest/gcb/grid/findRegions',
                   			contentType : 'application/json;charset=UTF-8',
                   			success : function(result){
           						$('#regionID').combobox('loadData',result);
           						$('#searchfm').find('select[id=regionID]').combobox('loadData',result);
           					}
                   		}
                   );
               break;  
			 }
		 },
	};
}();

squareActivity.config = {
		grid : {
		id : "grid",
		type : "easyui-datagrid",
		option : {
			url : ctxRest + '/grid/squareActivity/list',
			contentType : 'application/json;charset=utf-8',
			fit : true,
			fitColumns : true,
			rownumbers : false,
			nowrap : true,
			striped : true,
			pageList : [ 10, 15, 20, 30 ],
			pageSize : 20,
			pagination : true,
			remoteSort : false,
			singleSelect : true,
			border : false,
			idField : 'id',
			columns : [ [ {
				title : 'id',
				field : 'id',
				hidden : 'true',
				width : 50
			},{
				title : '编号',
				field : 'regNO',
				width : 80
			},{
				title : '登记日期',
				field : 'regDate',
				width : 80
			},{
				title : '申请单位(个人)',
				field : 'company',
				width : 120
			},{
				title : '申请人',
				field : 'proposer',
				width : 80
			}, {
				title : '广场名称',
				field : 'gridName',
				width : 100
			}, {
				title : '区域名称',
				field : 'regionName',
				width : 100
			}, {
				title : '活动时间',
				field : 'startTime',
				width : 100
			}, {
				title : '宣传性质',
				field : 'type',
				width : 100,
				formatter : function(value, row, index){
					if (value == '1') {
						return '商业性';
					} else if (value == '2'){
						return '公益性';
					}
				}
			}, {
				title : '操作',
				field : 'options',
				width : 100,
				formatter : function(value, row, index){
					return  '<a href="javascript:squareActivity.showEditForm('+"'"+row.id+"'"+')">[查看详细]</a>';
				}
			
			} ] ],
			toolbar : '#toolbar'
		}
	},
	newDlg : {
		id : 'newDlg',
		type : 'easyui-dialog',
		style : 'padding:5px;',
		option : {
			title : '广场活动登记信息',
			width : 800,
			height : 450,
			closed : true,
			modal : true,
			buttons : [
			           {text:'保存',iconCls:'icon-ok',handler:function(){squareActivity.save();}},
			           {text:'取消',iconCls:'icon-cancel',handler:function(){$('#newDlg').dialog('close');}}
			           ]
		},
		controls : [
		           {
		        	   id : 'fm',
		        	   type : 'form',
		        	   isExternal : true,
		        	   layout : {
		        		   captionWidth: 150,
		        		   fieldWidth	 : 200,
		        		   fieldHeight : 30,
		        		   rowHeight	 : 40,
		        		   columns	 : 2,
		        		   controls : [ 
		        		                {id:'regNO',caption:'编号',type:'easyui-textbox',option:{required: true}}, 
		        		                {id:'regDate',caption:'登记日期',type:'easyui-datebox',option:{editable:false , required: true}},
		        		                {id:'company',caption:'申请单位(个人)',type:'easyui-textbox',option:{required: true} ,colspan:2},
		        		                {id:'proposer',caption:'申请个人',type:'easyui-textbox',option:{required: true}},
		        		                {id:'tel',caption:'电话号码',type:'easyui-textbox',option:{required: true}},
		        		                {id:'content',caption:'宣传内容',type:'easyui-textbox',colspan:2,option:{height:80,multiline:true}},
		        		                {id : 'gridID',caption : '广场名称',type : 'easyui-combobox',
		        							option : {
		        								required : true,
		        								url : ctx + '/rest/gcb/grid/findList',
		        								method : 'post',
		        								textField : 'name',
		        								valueField : 'id',
		        								editable : false,
		        								onSelect: function (record)
		        								{
		        									squareActivity.handleSelect(record);
		        								},
		        							}
		        						},{
		        							id : 'regionID',
											caption : '区域名称',
											type : 'easyui-combobox',
											option : {
												required : true,
												url : ctx + '/rest/gcb/grid/findRegions',
												method : 'post',
												textField : 'name',
												valueField : 'id',
												editable : false,
												onLoadSuccess:function()
												{
												  var data = $('#regionID').combobox('getData');
												  $('#regionID').combobox('select',data[0].id);
												}
											}
		        						},
		        		                {id:'startTime',caption:'活动时间',type:'easyui-datebox',option:{editable:false , required: true}},
		        		                {id:'type',caption:'宣传性质',type:'easyui-combobox',option:{
		        		        			data:[{id : '1', text : '商业性'},{id : '2', text : '公益性'}],
		        		        			textFiled : 'text',
		        		        			valueField: 'id',
		        		        			editable : false, 
		        		        			required: true
		        						}},
		        						{id:'length',caption:'占用面积  长',type:'easyui-textbox'},
		        						{id:'width',caption:'宽',type:'easyui-textbox'},
		        		                {id:'id',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
		        					  ]
		        	   }
		           }
		           ]
	},
	searchDlg : {
		id : 'searchDlg',
		type : 'easyui-dialog',
		style : 'padding:5px;',
		option : {
			title : '查询',
			width : 750,
			height : 380,
			closed : true,
			modal : true,
			buttons : [
			           {text:'查询',iconCls:'icon-ok',handler:function(){squareActivity.search();}},
			           {text:'取消',iconCls:'icon-cancel',handler:function(){$('#searchDlg').dialog('close');}},
			           {text:'重置',iconCls:'icon-clear',handler:function(){squareActivity.clearSearch();}}
			           ]
		},
		controls : [
		           {
		        	   id : 'searchfm',
		        	   type : 'form',
		        	   isExternal : true,
		        	   layout : {
		        		   captionWidth: 150,
		        		   fieldWidth	 : 200,
		        		   fieldHeight : 30,
		        		   rowHeight	 : 40,
		        		   columns	 : 2,
		        		   controls : [ 
		        		                {id:'regNO',caption:'编号',type:'easyui-textbox',option:{width:200},colspan:2},
		        		                {id:'regDateStart',caption:'登记日期开始',type:'easyui-datebox',option:{editable:false}},
		        		                {id:'regDateEnd',caption:'登记日期结束',type:'easyui-datebox',option:{editable:false}},
		        		                {id:'company',caption:'申请单位(个人)',type:'easyui-textbox'},
		        		                {id:'proposer',caption:'申请个人',type:'easyui-textbox'},
		        		                {id : 'gridID',caption : '广场名称',type : 'easyui-combobox',
		        							option : {
		        								url : ctx + '/rest/gcb/grid/findList',
		        								method : 'post',
		        								textField : 'name',
		        								valueField : 'id',
		        								editable : false,
		        								onSelect: function (record)
		        								{
		        									squareActivity.handleSelect(record);
		        								},
		        							}
		        						},{
		        							id : 'regionID',
											caption : '区域名称',
											type : 'easyui-combobox',
											option : {
												url : ctx + '/rest/gcb/grid/findRegions',
												method : 'post',
												textField : 'name',
												valueField : 'id',
												editable : false
											}
		        						},
		        		                {id:'startTimeStart',caption:'活动时间开始',type:'easyui-datebox',option:{editable:false }},
		        		                {id:'startTimeEnd',caption:'活动时间结束',type:'easyui-datebox',option:{editable:false }},
		        		                {id:'type',caption:'宣传性质',type:'easyui-combobox',option:{
		        		        			data:[{id : '1', text : '商业性'},{id : '2', text : '公益性'}],
		        		        			textFiled : 'text',
		        		        			valueField: 'id',
		        		        			editable : false 
		        						}}
		        					  ]
		        	   }
		           }
		           ]
	},

};


